package com.zzj.ecology.admin.controller.system;

import com.zzj.ecology.admin.controller.BaseController;
import com.zzj.ecology.common.constant.UrlConstants;
import com.zzj.ecology.common.enums.BusinessType;
import com.zzj.ecology.framework.annotation.Log;
import com.zzj.ecology.framework.annotation.RepeatSubmit;
import com.zzj.ecology.system.bean.system.SysPermission;
import com.zzj.ecology.system.bean.vo.dtree.DTreeResponse;
import com.zzj.ecology.system.domain.Page;
import com.zzj.ecology.system.dto.HttpResult;
import com.zzj.ecology.system.service.system.SysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Api(value = "SysPermission", description = "SysPermission权限相关操作接口")
@RestController
public class SysPermissionController extends BaseController {

    @Autowired
    private SysPermissionService sysPermissionService;


    /**
     * 查询所有 权限 数据, slect box 下拉框数据
     * @return
     */
    @Deprecated
    @PostMapping(value = UrlConstants.PermissionUrl.SELECT_BOX_LIST)//  /ajax/admin/sysPermission/findPermissionListSelectDisplay
    public HttpResult findPermissionListSelectDisplay() {
        List<SysPermission> sysPermissions = sysPermissionService.findPermissionListSelectDisplay();
        return successData(sysPermissions);
    }

    @ApiOperation(value = "查询分页数据", notes = "分页数据", httpMethod = "GET")
    @RequestMapping(value = UrlConstants.PermissionUrl.LIST)
    public HttpResult findPageList(Page page) {
        HttpResult result = sysPermissionService.findPageList(page);
        return result;
    }

    /**
     * 查询 Dtree 数据结构,用于前端 下拉框选择, 查询 delete_status = '0' 表示未删除的数据
     * @return
     */
    @ApiOperation(value = "查询权限下拉框", notes = "查询权限集合数据,用于前端下拉框等显示使用", httpMethod = "POST")
    @PostMapping(value = UrlConstants.PermissionUrl.DTREE)
    public DTreeResponse dtree() {
        DTreeResponse response = sysPermissionService.dtree();
        return response;
    }

    @RepeatSubmit
    @Log(title = "权限操作",businessType = BusinessType.INSERT)
    @ApiOperation(value = "创建新的权限", notes = "添加一个新的权限记录，并指示上级权限", httpMethod = "POST")
    @PostMapping(value = UrlConstants.PermissionUrl.CREATE)
    public HttpResult create(SysPermission sysPermission) {
        if (sysPermission.getParentId() == null) {
            sysPermission.setParentId(0);
        }
        sysPermission.setCreateAt(new Date());
        sysPermission.setCreateBy(getUsername());
        System.out.println(sysPermission);
        HttpResult result = sysPermissionService.create(sysPermission);
        return success(result);
    }

    @RepeatSubmit
    @Log(title = "权限操作",businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改权限", notes = "根据权限修改权限", httpMethod = "POST")
    @PostMapping(value = UrlConstants.PermissionUrl.UPDATE)
    public HttpResult update(SysPermission sysPermission) {
        if (sysPermission.getParentId() == null) {
            sysPermission.setParentId(0);
        }
        sysPermission.setUpdateAt(new Date());
        sysPermission.setUpdateBy(getUsername());
        HttpResult result = sysPermissionService.update(sysPermission);
        return success(result);
    }

    @RepeatSubmit
    @ApiOperation(value = "批量删除用户", notes = "根据用户的ID或IDS, 删除用户。", httpMethod = "POST")
    @Log(title = "权限操作",businessType = BusinessType.DELETE)
    @PostMapping(value = UrlConstants.PermissionUrl.DELETE_BY_IDS)
    public HttpResult deleteByIds(@RequestParam("ids") List<Integer> userids) {
        //String username = SecurityUtils.getUsername();//((User) SecurityUtils.getPrincipal()).getUsername();
        if(userids.size() <= 0) return error();
        HttpResult result = sysPermissionService.updateDeleteStatusByIds(userids, getUsername());
        return success(result);
    }

    @ApiOperation(value = "根据URLID关联查询Permission", notes = "根据URL的主键，查询关联的Permission权限。", httpMethod = "POST")
    @PostMapping(value = UrlConstants.PermissionUrl.FIND_BY_URLID)
    public HttpResult findByUrlId(Integer urlId) {
        SysPermission sysPermission = sysPermissionService.findByUrlId(urlId);
        return HttpResult.success().add(HttpResult.DATA_TAG, sysPermission);
    }














//
//    /**
//     * 查询所有TOP级权限, 包括子集
//     * @return
//     */
//    //@PostMapping(value = "/admin/sysPermission/ajax/findParentsListDetail")
//    public HttpResult findParentsListDetail() {
//        HttpResult result = sysPermissionService.findParentsListDetail();
//        return result;
//    }
//
//    //@PostMapping(value = "/admin/sysPermission/ajax/findPermissionsByRoleId")
//    public HttpResult findPermissionsByRoleId(Integer roleId) {
//        if (roleId == null) {
//            return HttpResult.error();
//        }
//        HttpResult result = sysPermissionService.findListByRoleId(roleId);
//        return result;
//    }
//

//
//    /**
//     * 查询 所有 非 TOP 权限, 不映射子集，父类，映射普通字段。即 pid != 0
//     * @return
//     */
//    //@PostMapping(value = "/admin/sysPermission/ajax/findWithoutParentsList")
//    public HttpResult findWithoutParentsList() {
//        HttpResult result = sysPermissionService.findWithoutParentsList();
//        return result;
//    }
//
//    /**
//     * 创建
//     * @param sysPermission
//     * @return
//     */
//    @PostMapping(value = "/admin/sysPermission/ajax/create")
//    public HttpResult create(SysPermission sysPermission) {
//        HttpResult result = sysPermissionService.create(sysPermission);
//        return result;
//    }
//
//    /**
//     * 修改 无关联修改
//     * @param sysPermission
//     * @return
//     */
//    @PostMapping(value = "/admin/sysPermission/ajax/update")
//    public HttpResult update(SysPermission sysPermission) {
//        HttpResult result = sysPermissionService.update(sysPermission);
//        return result;
//    }
//
//
//
//    /**
//     * 根据前端提供的 permissionCode 查询数据库是否存在，不存在返回 true
//     * @param permissionCode
//     * @return
//     */
//    @PostMapping(value = "/admin/sysPermission/ajax/isNotExistPermissionCode")
//    public boolean isNotExistRoleCode(String permissionCode) {
//        if (StringUtils.isEmpty(permissionCode)) {
//            return false;
//        }
//        boolean isNoExist = sysPermissionService.isNotExistRoleCode(permissionCode);
//        return isNoExist;
//    }
//
//    /**
//     * 删除权限 及 权限对应的关联
//     * @param ids
//     * @return
//     */
//    @PostMapping(value = "/admin/sysPremission/ajax/deleteAndUnGrantByIds")
//    public HttpResult deleteAndUnGrantByIds(@RequestParam("ids") Integer[] ids) {
//        if (ids == null || ids.length == 0) {
//            return HttpResult.error();
//        }
//        HttpResult result = sysPermissionService.deleteAndUnGrant(ids);
//        return result;
//    }



}
